﻿//-----------------------------------------------------------------------
// <copyright file="Permission.cs" company="FPTSS">
//     Copyright FPTSS. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace ExtSite
{
    using System.Linq;
    using System.Collections.Generic;
    using ExtSite.Classes;

    /// <summary>
    /// Set permission page of login user
    /// </summary>
    public class ClsPermission
    {
        #region PAGE
       
        /// <summary>
        /// Get permission on page of user
        /// </summary>
        /// <param name="userid">user id</param>
        /// <param name="pageurl">page url</param>
        /// <returns>string permission</returns>
        public static string GetPagePermission(int userid, int menuid)
        {

            string strPermission = "00000000";
            var db = new DataConfigClassesDataContext();
            Menu p = db.Menus.SingleOrDefault(m => m.ID == menuid);
            if (p == null)
                return strPermission;
            int pageid = p.ID;
            MenuOfUser mou = db.MenuOfUsers.SingleOrDefault(m => m.MenuID == menuid && m.UserID == userid);
            if (mou != null)
            {
                strPermission = GetStringPagePermission(mou);
            }
            

            return strPermission;
        }      

        /// <summary>
        /// Check view permission
        /// </summary>
        /// <param name="strPermission">permission string</param>
        /// <returns>true, false</returns>
        public static bool HasView(string strPermission)
        {
            return strPermission.Substring(0, 1) == "1";
        }

        /// <summary>
        /// Check New permission
        /// </summary>
        /// <param name="strPermission">permission string</param>
        /// <returns>true, false</returns>
        public static bool HasNew(string strPermission)
        {
            return strPermission.Substring(1, 1) == "1";
        }

        /// <summary>
        /// Check Delete permission 
        /// </summary>
        /// <param name="strPermission">permission string</param>
        /// <returns>true, false</returns>
        public static bool HasDelete(string strPermission)
        {
            return strPermission.Substring(2, 1) == "1";
        }

        /// <summary>
        /// Check Edit permission
        /// </summary>
        /// <param name="strPermission">permission string</param>
        /// <returns>true, false</returns>
        public static bool HasEdit(string strPermission)
        {
            return strPermission.Substring(3, 1) == "1";
        }

        /// <summary>
        /// Check view permission
        /// </summary>
        /// <param name="strPermission">permission string</param>
        /// <returns>true, false</returns>
        public static bool HasLView(string strPermission)
        {
            return strPermission.Substring(4, 1) == "1";
        }

        /// <summary>
        /// Check New permission
        /// </summary>
        /// <param name="strPermission">permission string</param>
        /// <returns>true, false</returns>
        public static bool HasLNew(string strPermission)
        {
            return strPermission.Substring(5, 1) == "1";
        }

        /// <summary>
        /// Check Delete permission 
        /// </summary>
        /// <param name="strPermission">permission string</param>
        /// <returns>true, false</returns>
        public static bool HasLDelete(string strPermission)
        {
            return strPermission.Substring(6, 1) == "1";
        }

        /// <summary>
        /// Check Edit permission
        /// </summary>
        /// <param name="strPermission">permission string</param>
        /// <returns>true, false</returns>
        public static bool HasLEdit(string strPermission)
        {
            return strPermission.Substring(7, 1) == "1";
        }

        /// <summary>
        /// permission to string
        /// </summary>
        /// <param name="pou">row of CompanyOfUser in database</param>
        /// <returns>permission string</returns>
        private static string GetStringPagePermission(MenuOfUser mou)
        {

            /// VIEW|NEW|DELETE|EDIT
            string strPermission = "00000000";
            string strview = "1";
            string strnew = "1";
            string strdelete = "1";
            string stredit = "1";
            string strlview = "1";
            string strlnew = "1";
            string strldelete = "1";
            string strledit = "1";
            if (mou.View == false)
            {
                strview = "0";
            }
            if (mou.New == false)
            {
                strnew = "0";
            }
            if (mou.Delete == false)
            {
                strdelete = "0";
            }
            if (mou.Edit == false)
            {
                stredit = "0";
            }
            if (mou.LView == false)
            {
                strlview = "0";
            }
            if (mou.LNew == false)
            {
                strlnew = "0";
            }

            if (mou.LDelete == false)
            {
                strldelete = "0";
            }

            if (mou.LEdit == false)
            {
                strledit = "0";
            }

            strPermission = strview + strnew + strdelete + stredit + strlview + strlnew + strldelete + strledit;
            return strPermission;
        }
        #endregion PAGE
    }
}
